<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * manage Proimg
 *
 * @package     manage
 * @subpackage  
 * @category    Proimg -  产品图片
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class Proimg extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	  	$this->load->library('form_validation');   // 表单验证类
		$this->load->model('product_model','product');       // 加载产品模型
		$this->load->model('proimg_model','proimg');     // 加载产品属性模型 
		check_login(); // 检查是否登录
	}

	/**
	 * 
	 * @return [type] [list]
	 */
	public function index()
	{
		$total = $this->proimg->get_num_rows();
		if($total != 0){
			// 加载分页类
			$arr = array('page_total_num' => $total,'every_page_num'=>20,'page_url_name'=>'page_id');
			$this->load->library('page',$arr);
			$data['pager']	  = $this->page->pages_all();
			$data['arr'] 	  = $this->proimg->get_all_data_and_page($this->page->limit);
			$data['imgurl']   = 'http://qdb.oss-cn-qingdao.aliyuncs.com/proimg/';
		}else{
			$data['error'] = '暂无产品图片!';
		}
		$this->load->view('admin/proimg/index',$data);
	}

	/**
	 * 增加数据
	 */
	public function insert()
	{
		$product = $this->product->to_options(); // 所有产品列表
		if ($this->form_validation->run('proimg') == FALSE){
		   $this->load->view('admin/proimg/insert',compact("product"));
		}else{
			if(!empty($_POST['sort'])){
				$arr = $this->input->post();
				$_insert['pro_id'] 			= $arr['pro_id'];
				$_insert['img_sort'] 			= $arr['sort'];
				$_insert['img_status'] 			= $arr['status'];
				if(!empty($_FILES['url']['name'])){ 	// 判断是否上传缩略图
					$this->load->library('myupload',array('name'=>'url'));   // 加载上传类
					if($this->myupload->start()){
						$this->load->library('oss');   					   // 加载oss类
					    $clientName 	=  $this->myupload->FileName;      // 图片名称
					    $filePath   =  $this->myupload->getRealPath();     // 临时上传路径
					    $this->oss->upload('proimg/'.$arr['pro_id'].'/'.$clientName,$filePath);
					    $_insert['img_url'] = $clientName; 			       // 图片名称
					}
				}
				if($this->proimg->insert($_insert)){
					$this->sys_message->skip('增加成功',site_url('manage/proimg'));
				}else{
					$this->sys_message->show_error('增加失败,请重新添加');
				}
			}
		}
	}

	/**
	 * 编辑数据信息
	 */
	public function edit()
	{
		// 默认值
		$data['arr']      = $this->proimg->get_a_data_by_aid($this->input->get('id'));
		$data['product']  = $this->product->to_options($data['arr']['pro_id']); // 栏目数据
		$data['imgurl']   = 'http://qdb.oss-cn-qingdao.aliyuncs.com/proimg/';
		if ($this->form_validation->run('proimg') == FALSE){
		    $this->load->view('admin/proimg/edit',$data);
		}else{
			if(!empty($_POST['sort'])){
				$arr = $this->input->post();
				$_update['pro_id'] 			= $arr['pro_id'];
				$_update['img_sort'] 			= $arr['sort'];
				$_update['img_status'] 			= $arr['status'];
				if(!empty($_FILES['url']['name'])){ 	// 判断是否上传缩略图
					$this->load->library('myupload',array('name'=>'url'));   // 加载上传类
					if($this->myupload->start()){
						$this->load->library('oss');   					   // 加载oss类
					    $clientName 	=  $this->myupload->FileName;      // 图片名称
					    $filePath   =  $this->myupload->getRealPath();     // 临时上传路径
					    $this->oss->upload('proimg/'.$arr['pro_id'].'/'.$clientName,$filePath);
					    $_update['img_url'] = $clientName; 			       // 图片名称
					}
				}
				if($this->proimg->upload($_update,$_GET['id'])){
					$this->sys_message->skip('编辑成功',site_url('manage/proimg'));
				}else{
					$this->sys_message->show_error('增加失败,请重新编辑!');
				}
			}


		}
	}

	/**
	 * 删除文章
	 */
	public function delete()
	{
		if(!empty($_GET['id']) && intval($_GET['id'])){
			if($this->proimg->delete($_GET['id'])){
				die('ok');
			}else{
				die('no');
			}
		}else{
			die('error_1');
		}
	}


}